$OpenBSD: patch-Source_cmGeneratorTarget_cxx,v 1.6 2016/11/28 09:15:13 dcoppa Exp $
--- Source/cmGeneratorTarget.cxx.orig	Fri Nov 11 15:37:14 2016
+++ Source/cmGeneratorTarget.cxx	Fri Nov 25 22:31:45 2016
@@ -1094,11 +1094,15 @@ std::string cmGeneratorTarget::GetCompilePDBPath(
 
 bool cmGeneratorTarget::HasSOName(const std::string& config) const
 {
+#if !defined(__OpenBSD__)
   // soname is supported only for shared libraries and modules,
   // and then only when the platform supports an soname flag.
   return ((this->GetType() == cmState::SHARED_LIBRARY) &&
           !this->GetPropertyAsBool("NO_SONAME") &&
           this->Makefile->GetSONameFlag(this->GetLinkerLanguage(config)));
+#else
+  return false;
+#endif
 }
 
 bool cmGeneratorTarget::NeedRelinkBeforeInstall(
@@ -2810,9 +2814,15 @@ void cmGeneratorTarget::GetLibraryNames(std::string& n
   // Check for library version properties.
   const char* version = this->GetProperty("VERSION");
   const char* soversion = this->GetProperty("SOVERSION");
+#if defined(__OpenBSD__)
+  if (this->GetType() != cmState::SHARED_LIBRARY &&
+      this->GetType() != cmState::MODULE_LIBRARY)
+#else
   if (!this->HasSOName(config) ||
       this->Makefile->IsOn("CMAKE_PLATFORM_NO_VERSIONED_SONAME") ||
-      this->IsFrameworkOnApple()) {
+      this->IsFrameworkOnApple())
+#endif
+  {
     // Versioning is supported only for shared libraries and modules,
     // and then only when the platform supports an soname flag.
     version = CM_NULLPTR;
@@ -2837,6 +2847,34 @@ void cmGeneratorTarget::GetLibraryNames(std::string& n
   // The library name.
   name = prefix + base + suffix;
 
+#if defined(__OpenBSD__)
+  // Override shared library version using LIBxxx_VERSION
+  // environment variable. Needed for OpenBSD ports system.
+  if (this->GetType() == cmState::SHARED_LIBRARY ||
+      this->GetType() == cmState::MODULE_LIBRARY) {
+    std::string env_name = "LIB" + base + "_VERSION";
+    char *env_vers_cstr = getenv(env_name.c_str());
+
+    if (env_vers_cstr && strlen(env_vers_cstr) > 0) {
+      // This means an override is present.
+      std::string env_vers = std::string(env_vers_cstr);
+
+      size_t first = env_vers.find_first_of(".");
+      size_t last = env_vers.find_last_of(".");
+
+      if ((first != last) || (first == std::string::npos)) {
+        std::string msg = "Bad ";
+        msg += env_name;
+        msg += " specification: ";
+        msg += env_vers;
+        this->LocalGenerator->IssueMessage(cmake::INTERNAL_ERROR, msg);
+      } else {
+        version = env_vers_cstr;
+      }
+    }
+  }
+#endif
+
   if (this->IsFrameworkOnApple()) {
     realName = prefix;
     if (!this->Makefile->PlatformIsAppleIos()) {
@@ -2848,7 +2886,11 @@ void cmGeneratorTarget::GetLibraryNames(std::string& n
     soName = realName;
   } else {
     // The library's soname.
+#if !defined(__OpenBSD__)
     this->ComputeVersionedName(soName, prefix, base, suffix, name, soversion);
+#else
+    this->ComputeVersionedName(soName, prefix, base, suffix, name, version);
+#endif
 
     // The library's real name on disk.
     this->ComputeVersionedName(realName, prefix, base, suffix, name, version);
@@ -3943,7 +3985,21 @@ void cmGeneratorTarget::ComputeVersionedName(std::stri
   vName = this->Makefile->IsOn("APPLE") ? (prefix + base) : name;
   if (version) {
     vName += ".";
+#if defined(__OpenBSD__)
+    // OpenBSD-style versioning for shared libraries.
+    // Convert libname.so.X.X.X to libname.so.X.X
+    int j = 0;
+    for (int i = 0; i < (int)strlen(version); i++) {
+      if (version[i] == '.') {
+        j++;
+        if (j == 2)
+          break;
+      }
+      vName += version[i];
+    }
+#else
     vName += version;
+#endif
   }
   vName += this->Makefile->IsOn("APPLE") ? suffix : std::string();
 }
